package com.onpositive.wiki3.catmodels;

import java.util.Arrays;
import java.util.HashSet;

import com.onpositive.wiki3.db.catmodel.Category;
import com.onpositive.wiki3.db.catmodel.matches.classify.AbstractRecordingMatcher;
import com.onpositive.wiki3.db.catmodel.matches.classify.RelationConstants;
import com.onpositive.wiki3.db.catmodel.matches.classify.RelationStore;

public class ParentWriter extends AbstractRecordingMatcher {

	public ParentWriter(RelationStore store) {
		super(store);
	}

	@Override
	public int accept(Category st) {
		ICategoryModel model = ModelTagger.getModel(st);
		if (toRecord.get(RelationConstants.IS_A).get(st.id())==null){
			return 0;
		}
		if (!(model instanceof PluralCatModel)){
			return 0;
		}
		
		Category[] parents = st.getParentsCategories();
		HashSet<Category> allParents = new HashSet<Category>(
				Arrays.asList(parents));
		l2: for (Category z : parents) {
			model = ModelTagger.getModel(z);
			if (model instanceof SystemCategoryModel){
				continue;
			}
			Category[] parentsCategories = z.getParentsCategories();
			for (Category m : parentsCategories) {
				if (allParents.contains(m)) {
					if (toRecord.get(RelationConstants.IS_A).get(m.id())!=null){
						record(RelationConstants.IS_A, z, st);
					}
					continue l2;
				}
			}
		}
		return 0;
	}

}
